/**
 * Created by Maibenben on 2017/5/5.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid = $.cookie("mid");
    var token = $.cookie("token");
    var databack = false;
    var important = "&mid=" + mid + "&token=" + token;
    //进入页面显示待支付的信息
    $.get(payURL + "?status=0" + "&mid=" + mid + "&token=" + token, function (data) {
        databack = true;
        //模板渲染
        var listHTML = template("listTem0", data.data);
        $(".load").css("display", "none");
        $(".list").html(listHTML);
        /*
         //懒加载
         var next_page = data.data.next_page_url;
         if (next_page) {
         lazyload(function () {
         $.get(next_page + "&mid=" + mid + "&token=" + token, function (res) {
         next_page = res.data.next_page_url;
         });
         });
         }else{
         creatLayout("");
         }
         */
        var childs = $('.list').find('li');
        if (childs.length == 0) {
            $('.list').append('<p style="text-align: center;padding-top: 10px;font-size:15px; line-height: 360px; color: #666;">暂无数据</p>');
        }

        //设置取消带支付订单
        $(".gray").click(function () {
            var jh = $(this).attr("data-id");
            //要进行弹框确认
            $("#header").css({position: "fixed", left: 0, top: 0});
            $(".tip").css("display", "block");
            $(".no").click(function () {
                $(".tip").css("display", "none");
                $("#header").css({position: "relative", left: 0, top: 0});
            });
            $(".yes").click(function () {
                $(".tip").css("display", "none");
                $.post(delAppointURL, {
                    mid: mid,
                    token: token,
                    id: jh
                }, function (res) {
                    if (res['state'] == 1) {
                        location.reload();
                    } else {
                        creatLayout("删除失败");
                    }
                });
            });
        });
        //倒计时功能
        $(".orange").each(function (index) {
            var aid = $(this).attr("data-id");
            if ($(this).attr("data-remain") < 0) {
                $(this).css({color: '#777', border: '1px solid #777'});
            } else {
                var current = $(this);
                var that = $(this).parent().parent().find("#wait_time");
                var last_time = $(this).attr("data-remain");
                var currentTimer = setInterval(function () {
                    last_time -= 0.1;
                    var h = Math.floor(last_time / 3600);
                    var m = Math.floor((last_time - h * 3600 ) / 60);
                    var s = Math.floor(last_time - h * 3600 - m * 60);
                    m = m > 9 ? m : '0' + m;
                    s = s > 9 ? s : '0' + s;
                    that.text('待支付 ' + m + ' : ' + s);
                    if (last_time <= 0) {
                        $(".status").eq(index).html("已过期");
                        current.parents().parents().remove();
                        $.get(expireURL + "?aid=" + aid + important, function (res) {
                            if (res.state == 1) {
                                current.off("click").css({color: '#777', border: '1px solid #777'});
                            }
                        });
                        clearInterval(currentTimer);
                    }
                }, 100);
                $(this).on("click", function () {
                    window.location.href = 'purchase.html?aid=' + aid;
                });
            }
        });
    });
    //点击待支付
    $(".tab p:eq(0)").on("click", function () {
        location.reload();
    });
    //点击待就诊
    $(".tab p:eq(1)").on('click', function () {
        if (databack) {
            $(".list").html("");
            $(".load").css("display", "block");
            $(this).addClass("active").siblings("p").removeClass("active");
            databack = false;
            $.get(payURL + "?status=1&mid=" + $.cookie("mid") + "&token=" + $.cookie('token'), function (data) {
                databack = true;
                $(".load").css("display", "none");
                var listHTML = template("listTem1", data.data);
                $(".list").html(listHTML);
                var childs = $('.list').find('li')
                if (childs.length == 0) {
                    $('.list').append('<p style="text-align: center;padding-top: 10px;font-size:15px; line-height: 360px; color: #666;">暂无数据</p>');
                }
                $(".Remaining_time").each(function () {
                    if ($(this).attr("data-wiat-remain") < 0) {
                        $(this).parent().parent().next("li").remove();
                        $(this).parent().text('已逾期：请到窗口处退款').css("color", "red");
                        $(this).parent().parent().next().children("p").css({"color": "red", "border": "none"});
                    } else {
                        var remainTime = $(this).attr("data-wiat-remain");
                        var rd = Math.floor(remainTime / 86400);
                        var rh = Math.floor((remainTime - rd * 86400) / 3600);
                        var rm = Math.floor((remainTime - rd * 86400 - rh * 3600) / 60);
                        $(this).text(rd + "天" + rh + '小时' + rm + '分');
                    }
                });
                $(".orange").on("click", function () {
                    window.location.href = "index_change.html?id=" + $(this).attr("data-order-id");
                });
            });
        }
    });
    //点击待评价
    $(".tab p:eq(2)").on('click', function () {
        if (databack) {
            $(this).addClass("active").siblings("p").removeClass("active");
            databack = false;
            $(".list").html("");
            $(".load").css("display", "block");
            $.get(payURL + "?status=2&mid=" + $.cookie("mid") + "&token=" + $.cookie('token'), function (data) {
                databack = true;
                $(".load").css("display", "none");
                var listHTML = template("listTem2", data.data);
                $(".list").html(listHTML);
                var childs = $('.list').find('li')
                if (childs.length == 0) {
                    $('.list').append('<p style="text-align: center;padding-top: 10px;font-size:15px; line-height: 360px; color: #666;">暂无数据</p>')
                }
                $(".evaluate").on("click", function () {
                    window.location.href = "comment.html?id=" + $(this).attr("data-docid") + "&aid=" + $(this).attr("data-id");
                });
            });
        }
    });
    //点击已完成
    $(".tab p:eq(3)").on('click', function () {
        if (databack) {
            $(this).addClass("active").siblings("p").removeClass("active");
            databack = false;
            $(".list").html("");
            $(".load").css("display", "block");
            $.get(payURL + "?status=3&mid=" + $.cookie("mid") + "&token=" + $.cookie('token'), function (data) {
                $(".load").css("display", "none");
                databack = true;
                var listHTML = template("listTem3", data.data);
                $(".list").html(listHTML);
                var childs = $('.list').find('li')
                if (childs.length == 0) {
                    $('.list').append('<p style="text-align: center;padding-top: 10px;font-size:15px; line-height: 360px; color: #666;">暂无数据</p>')
                }
            });
        }
    });
});